package com.example.myapplication.tools;

import android.nfc.tech.MifareClassic;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcV;

import java.io.IOException;

/*
 @author cloin
 用法
 NfcV mNfcV = NfcV.get(tag);
 mNfcV.connect();
 NfcVUtil mNfcVutil = new NfcVUtil(mNfcV);
 取得UID
 mNfcVutil.getUID();
 读取block在1位置的内容
 mNfcVutil.readOneBlock(1);
 从位置7开始读2个block的内容
 mNfcVutil.readBlocks(7, 2);
 取得block的个数
 mNfcVutil.getBlockNumber();
 取得1个block的长度
 mNfcVutil.getOneBlockSize();
 往位置1的block写内容
 mNfcVutil.writeBlock(1, new byte[]{0, 0, 0, 0})
 */
public class NfcAUtil {

    private MifareClassic mNfcV;
    /**UID数组行式*/
    private byte[] ID;
    private String UID;

    /**
     * 初始化
     * @param mfc NfcV对象
     * @throws IOException
     */
    public NfcAUtil(MifareClassic mfc) throws IOException{
        this.mNfcV = mfc;
        ID = this.mNfcV.getTag().getId();
        byte[] uid = new byte[ID.length];
        int j = 0;
        for(int i = ID.length - 1; i >=0; i-- ){
            uid[j] = ID[i];
            j++;
        }
        this.UID = printHexString(uid);
    }
    public String getUID() {
        return UID;
    }


    /**
     * 写入AFI数据
     * @param b
     * @return
     * @throws IOException
     */
    public boolean setAFIState(byte b) throws IOException {
        byte[] cmd = new byte[11];
        cmd[0] = (byte) 0x22; // flag
        cmd[1] = (byte) 0x27; // command
        System.arraycopy(ID, 0, cmd, 2, ID.length); // UID
        cmd[10] = b;
        byte[] res = mNfcV.transceive(cmd);
        if(res[0] == 0x00){
            byte[] cmdNew = new byte[10];
            cmdNew[0] = (byte) 0x22; // flag
            cmdNew[1] = (byte) 0x2B; // command
            System.arraycopy(ID, 0, cmdNew, 2, ID.length); // UID

            return true;

            //千万不要锁定，不可逆
//            byte[] cmd2 = new byte[10];
//            cmd2[0] = (byte) 0x22; // flag
//            cmd2[1] = (byte) 0x28; // command
//            System.arraycopy(ID, 0, cmd2, 2, ID.length); // UID
//            byte[] res2 = mNfcV.transceive(cmd2);
//            if(res2[0] == 0x00){
//               return true;
//            }
        }
        return false;
    }

    /**
     * 写入DSFID数据
     * @param b
     * @return
     * @throws IOException
     */
    public boolean setDSFIDState(byte b) throws IOException {
        byte[] cmd = new byte[11];
        cmd[0] = (byte) 0x22; // flag
        cmd[1] = (byte) 0x29; // command
        System.arraycopy(ID, 0, cmd, 2, ID.length); // UID
        cmd[10] = b;
        byte[] res = mNfcV.transceive(cmd);
        if(res[0] == 0x00){
            byte[] cmdNew = new byte[10];
            cmdNew[0] = (byte) 0x22; // flag
            cmdNew[1] = (byte) 0x2B; // command
            System.arraycopy(ID, 0, cmdNew, 2, ID.length); // UID
            return true;

            //千万不要锁定，不可逆
//            byte[] cmd2 = new byte[10];
//            cmd2[0] = (byte) 0x22; // flag
//            cmd2[1] = (byte) 0x2A; // command
//            System.arraycopy(ID, 0, cmd2, 2, ID.length); // UID
//            byte[] res2 = mNfcV.transceive(cmd2);
//            if(res2[0] == 0x00){
//                return true;
//            }
        }
        return false;
    }

    /**
     * 读取一个位置在position的block
     * @param position 要读取的block位置
     * @return 返回内容字符串
     * @throws IOException
     */
    public String readOneBlock(int position) throws IOException{
        byte cmd[] = new byte[12];
        cmd[0] = (byte) 0x22;
        cmd[1] = (byte) 0x23;//0x20
        System.arraycopy(ID, 0, cmd, 2, ID.length); // UID
        cmd[10] = (byte) position;
        cmd[11] = (byte) 0;
        byte res[] = mNfcV.transceive(cmd);
        if(res[0] == 0x00){
            byte block[] = new byte[res.length - 1];
            System.arraycopy(res, 1, block, 0, res.length - 1);
            return printHexString(block);
        }
        return null;
    }



    /**
     * String转Hex格式Byte[]
     * @param hexString
     * @return
     */
    public byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    /**
     * 将byte[]转换成16进制字符串
     * @param data 要转换成字符串的字节数组
     * @return 16进制字符串
     */
    private String printHexString(byte[] data) {
        StringBuffer s = new StringBuffer();;
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            s.append(hex);
        }
        return s.toString();
    }

    //结果大写
    public static String ByteArrayToHexString(byte[] inarray) {
        int i, j, in;
        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                "B", "C", "D", "E", "F" };
        String out = "";
        for (j = inarray.length - 1; j >= 0 ; j--) {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }


    /**
     * 将数据写入到block,
     * @param position 要写内容的block位置
     * @param data 要写的内容,必须长度为blockOneSize
     * @return false为写入失败，true为写入成功
     * @throws IOException
     */
    public boolean writeBlock(int position, byte[] data) throws IOException{
        byte cmd[] = new byte[15];
        cmd[0] = (byte) 0x22;
        cmd[1] = (byte) 0x21;
        System.arraycopy(ID, 0, cmd, 2, ID.length); // UID
        //block
        cmd[10] = (byte) position;
        //value
        System.arraycopy(data, 0, cmd, 11, data.length);
        byte[]rsp = mNfcV.transceive(cmd);
        if(rsp[0] == 0x00)
            return true;
        return false;
    }

}
